package messages;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.UnknownHostException;

/**
 * @author Groupe RT-A1
 */
/**
 * Utilisee pour gerer la creation de messages
 */
public class MessagesFactory {

	private MessagesFactory() {}
	
	/**
	 * @param username
	 * @param waitAck
	 * @return byte[] 
	 * @throws MessageCreationException
	 */
	/** 
	 * Renvoie un byte[] contenant le Hello demande
	 */
	public static byte[] getHello(String username, boolean waitAck) throws MessageCreationException {
		Hello myHello;
		
		try {
			
			myHello = new Hello(username, waitAck);
			return createByte(myHello).toByteArray();
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
			throw new MessageCreationException("Impossible de déterminer votre nom d'hôte");
			
		} catch (IOException e) {
			e.printStackTrace();
			throw new MessageCreationException("Impossible de créer le byte[]");
		}
		
	}
	
	public static byte[] getChatMessage(String username, String msgToSend) throws MessageCreationException {
		ChatMessage myMessage;
		
		try {
			myMessage = new ChatMessage(username, msgToSend);
			return createByte(myMessage).toByteArray();

		} catch (UnknownHostException e) {
			e.printStackTrace();
			throw new MessageCreationException("Impossible de déterminer votre nom d'hôte");

		}
		 catch (IOException e) {
			e.printStackTrace();
			throw new MessageCreationException("Impossible de créer le byte[]");
		}
		
	}
	
	public static byte[] getBye(String username) throws MessageCreationException {
		Bye myBye;
		
		try {
			myBye = new Bye(username);
			return createByte(myBye).toByteArray();
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
			throw new MessageCreationException("Impossible de déterminer votre nom d'hôte");
		}
		 catch (IOException e) {
			e.printStackTrace();
			throw new MessageCreationException("Impossible de créer le byte[]");
		}
		
	}
	
	public static byte[] getFileNotif(String username, String nameFile, boolean request, boolean accepted) throws MessageCreationException {
		FileNotif myFileNotif;
		
		try {
			myFileNotif = new FileNotif(username, nameFile, request, accepted);
			return createByte(myFileNotif).toByteArray();
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
			throw new MessageCreationException("Impossible de déterminer votre nom d'hôte");
		}
		 catch (IOException e) {
			e.printStackTrace();
			throw new MessageCreationException("Impossible de créer le byte[]");
		}
		
	}
	
	/**
	 * @param myArray 
	 * @return Message
	 * @throws MessageCreationException
	 */
	/**
	 * Renvoie le message correspondant au byte[] passe en argument
	 */
	public static Message getMessage(byte[] myArray) throws MessageCreationException {
		ByteArrayInputStream byteAIS;
		ObjectInputStream ois;
		
		try {
			
			byteAIS = new ByteArrayInputStream(myArray);
			ois = new ObjectInputStream(byteAIS);
			Object monObject = ois.readObject();
			
			if (monObject instanceof Message) {
				return (Message)monObject;
			}
			else {
				throw new MessageCreationException("Le Byte[] ne correspond pas à un message connu");
			}
			
		} catch (IOException e) {
			e.printStackTrace();
			throw new MessageCreationException("Impossible de créer le message");
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new MessageCreationException("Impossible de créer le message");
		} 
			
	}
	
	/**
	 * @param myMessage
	 * @return ByteArrayOutputStream
	 * @throws IOException
	 */
	/**
	 * Renvoie un ByteArrayOutputStream contenant le message myMessage
	 */
	private static ByteArrayOutputStream createByte(Message myMessage) throws IOException {
		ByteArrayOutputStream byteAOS;
		ObjectOutputStream oos;
		
		byteAOS = new ByteArrayOutputStream();
		
		oos = new ObjectOutputStream(byteAOS);
		oos.writeObject(myMessage);
		oos.flush();
		oos.close();
		
		return byteAOS;
	}
	
}
